﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using outREACH.App_Code;
using System.Net.Mail;
using System.Configuration;

namespace outREACH
{
    public partial class forgetPass : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            List<Users> results = Users.GetUser();
            foreach (Users u in results)
            {
                if (TextBox1.Text == u.userName)
                {
                    Users u1 = Users.GetEmailDetails(TextBox1.Text.ToString());
                    if (u1.email.ToString().ToLower() != TextBox2.Text.ToString().ToLower())
                    {
                        Label3.Visible = true;
                    }
                    else
                    {
                        foreach(Users u2 in results)
                        {
                            if (u2.email.ToString().ToLower() != TextBox2.Text.ToString().ToLower())
                            {
                                Label5.Visible = true;
                            }
                            else
                            {
                            //SEND EMAIL TO DONOR ONCE TRRANSACTION IS SUCCESSFUL

                            MailMessage message = new MailMessage();
                            Image image = new Image();
                            image.ImageUrl = "~/Images/emaillogo.png";
                            message.From = new MailAddress("outreach360@gmail.com");
                            message.To.Add(new MailAddress(u.email.ToString()));
                            message.Subject = "outREACH360 - Your Password!";
                            message.Body = "Dear" + " " + u.firstName + " " + u.lastName + "," + "\n" + "Your Username is: " + u.userName + "\n" + "Your Password is: " + u.password + "\n" + "\nWith Regards," + "\nOutreach Team";


                            // SMTP Configuration
                            SmtpClient smtp = new SmtpClient();

                            smtp.Host = "smtp.gmail.com";
                            smtp.Port = 587;
                            smtp.EnableSsl = true;
                            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                            smtp.UseDefaultCredentials = false;
                            smtp.Credentials = new System.Net.NetworkCredential("outreach360@gmail.com", "360outreach");
                            // MAIL MESSAGE CONFIGURATION

                            // SEND EMAIL
                            smtp.Send(message);
                            }
                        }
                    }
                }
                else
                {
                    Label4.Visible = true;
                }
            }
            Response.Redirect("~/forgetPassRedirect.aspx");
        }

    }
}